<?php
namespace App\Utils\Other\Safepay\Declare;

use App\Exception\InternalException;
use App\Utils\Other\Safepay\App;
use Hyperf\Collection\Arr;

/**
 * Class Factory
 * @author zx
 * @package App\Utils\Other\Safepay\Payment
 */
Class Factory extends App {

    public function __construct()
    {
        parent::__construct();
        $this->setConfig(['prefix' => '/neopay/']);
        $this->setPath('customDeclaration');
    }

    /**
     * 付款
     * @param array $params
     * @return array [$request, $response, $params]
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function handle(array $params):array
    {
        return $this->request($params);
    }

    /**
     * 获取请求路径
     * @return string
     */
    public function getUrl():string
    {
        return $this->getConfig('domain').$this->getConfig('prefix').$this->getPath();
    }

    /**
     * 响应
     * @param $response
     * @param array $params
     * @return array
     */
    protected function response($response, array $params=[]):array
    {
        if (!isset($response['code']) || ($response['code'] != 0)) {
            $message = Arr::get($response, 'message');
            if (!$message) {
                throw new InternalException('response is null');
            }
            throw new InternalException($message);
        }

        $body = Arr::get($response['data']);

        return [
            $params,
            $response,
            $body,

        ];
    }
}